﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.JuZhuanAnZhiService;

namespace CommunitySystem
{
    public partial class JuZhuanAnZhiJiHuaControl : UserControl
    {
        private JuZhuanAnZhiServiceClient Client =
            new JuZhuanAnZhiServiceClient();
        public JuZhuanAnZhiJiHuaControl()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 0;
            Client.DeleteJuZhuanAnZhiJiHuaCompleted += new EventHandler<DeleteJuZhuanAnZhiJiHuaCompletedEventArgs>(Client_DeleteJuZhuanAnZhiJiHuaCompleted);
            Client.SuoJuZhuanAnZhiJiHuaCompleted += new EventHandler<SuoJuZhuanAnZhiJiHuaCompletedEventArgs>(Client_SuoJuZhuanAnZhiJiHuaCompleted);
            Load();
        }
        public void Load()
        {
            Client.GetZhaoLuJiHuaModelCompleted -= new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client_GetZhaoLuJiHuaModelCompleted);
            Client.GetZhaoLuJiHuaModelCompleted += new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client_GetZhaoLuJiHuaModelCompleted);
            Client.GetZhaoLuJiHuaModelAsync(0, islock);
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
            EditData.IsEnabled = false;
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            EditData.IsEnabled = true;
        }

        void Client_GetZhaoLuJiHuaModelCompleted(object sender, GetZhaoLuJiHuaModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            JuZhuanAnZhiJiHuaTitleWindow RMWindow = new JuZhuanAnZhiJiHuaTitleWindow(DateTime.Today.Year - nian.SelectedIndex, 0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as JuZhuanAnZhiJiHuaModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                JuZhuanAnZhiJiHuaTitleWindow RMWindow = new JuZhuanAnZhiJiHuaTitleWindow(DateTime.Today.Year - nian.SelectedIndex, seljw.ID, false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的计划！");
        }

        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as JuZhuanAnZhiJiHuaModel;
            if (seljw != null)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeleteJuZhuanAnZhiJiHuaAsync(seljw.ID);
                }
                else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的计划！");
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的计划！");
        }

        void Client_DeleteJuZhuanAnZhiJiHuaCompleted(object sender, DeleteJuZhuanAnZhiJiHuaCompletedEventArgs e)
        {
            Load();
        }

        private void btnSuoData_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定锁定本期工作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.SuoJuZhuanAnZhiJiHuaAsync();
            }
        }

        void Client_SuoJuZhuanAnZhiJiHuaCompleted(object sender, SuoJuZhuanAnZhiJiHuaCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("锁定成功！");
            Load();
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
        }
    }
}
